//
//  BwtBannerAd.m
//  bwt_ad_flutter
//
//  Created by 帅 on 2025/6/20.
//

#import "BwtBannerAd.h"
#import "BwtAdEventManager.h"
#import "BwtAdLogManager.h"
@interface BwtBannerAd()<CXHSDKBannerAdViewDelegate>



@end

@implementation BwtBannerAd

- (void)loadBannerAd{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIViewController *rvc = window.rootViewController;
    
    _bannerView = [[CXHAdSDKBannerAdView alloc] initWithFrame:CGRectMake(_adParams.adx.floatValue,_adParams.ady.floatValue, _adParams.adWidth.floatValue,_adParams.adHeight.floatValue)];
    _bannerView.delegate = self;
    _bannerView.controller = rvc;
    _bannerView.refershTime = 35;
    _bannerView.posId = _adParams.posId;
    _bannerView.customLoadingPage = YES;
    _bannerView.showCloseView = _adParams.showClose;
    // 查看价格，需要在设置广告位id之后调用
//    NSLog(@"price----%ld", [bannerView getBidPrice]);
    // 发送竞价失败上报，此时的价格为第三方赢价的价格
    // [_bannerView sendLossNotice:100];
    // 发送竞价成功上报
//    [bannerView sendWinNotice:100];
//    [_bannerView loadAndShow];
//    _bannerView.frame = CGRectMake(20, 200, 400, 100);
//    [rvc.view addSubview:_bannerView];
    [_bannerView loadAndShow];
}

/**
广告获取成功
@param bannerView banner实例
*/
- (void)CXH_bannerViewDidReceived:(CXHAdSDKBannerAdView *)bannerView{

    // 3 加载并显示广告 注意: 请确保banner视图显示在屏幕内的时候,调用load方法
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];

    [bwtAdEventManager postOnAdLoadEvent:_adParams];
}

/**
广告拉取失败
@param bannerView banner实例
@param error 错误描述
*/
- (void)CXH_bannerViewFailToReceived:(CXHAdSDKBannerAdView *)bannerView error:(NSError*)error{
    NSString *message = @"广告渲染失败了";
    dispatch_async(dispatch_get_main_queue(), ^{
        BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
        [bwtAdEventManager postOnAdLoadFailedEvent:self.adParams message:message];
        [self releaseAd];
    });
}
/**
广告点击
@param bannerView 广告实例
*/
- (void)CXH_bannerViewClicked:(CXHAdSDKBannerAdView *)bannerView loadingPageURL:(nonnull NSString *)loadingPageURL{
    [[BwtAdLogManager getBwtAdLogManager] log:@"banner广告被点击, adId: %@", _adParams.adId];
    /// 通知Flutter端
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager postOnAdClickEvent:_adParams];
}

/**
广告关闭
@param bannerView 广告实例
*/
- (void)CXH_bannerViewClose:(CXHAdSDKBannerAdView *)bannerView{
    [[BwtAdLogManager getBwtAdLogManager] log:@"banner广告被关闭, adId: %@", _adParams.adId];
    /// 通知Flutter端
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager postOnAdCloseEvent:_adParams];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self releaseAd];
    });
}


/**
广告展示
@param bannerView 广告实例
*/
- (void)CXH_bannerViewExposure:(CXHAdSDKBannerAdView *)bannerView{
    [[BwtAdLogManager getBwtAdLogManager] log:@"banner广告被展示, adId: %@", _adParams.adId];
    /// 通知Flutter端
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager postOnAdExposeEvent:_adParams];
}

/**
关闭落地页
@param bannerView 广告实例
*/
- (void)CXH_bannerAdCloseLandingPage:(nonnull CXHAdSDKBannerAdView *)bannerView {
    
}


- (void)CXH_bannerViewClickedReport:(nonnull CXHAdSDKBannerAdView *)bannerView { 
    
}


- (void)CXH_bannerViewExposureReport:(nonnull CXHAdSDKBannerAdView *)bannerView { 
    
}


- (void)releaseAd{
    if (self.bannerView) {
        [self.bannerView removeFromSuperview];
        _bannerView = nil;
        [[BwtAdLogManager getBwtAdLogManager] log:@"banner广告已释放, adId: %@", _adParams.adId];
    }
}

@end
